﻿/*
   Copyright 2012 University of Southampton

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace uk.ac.soton.ses.UserControls
{
    public class ExperimentSelectedEventArgs : EventArgs
    {
        public int SelectedExperimentID { get; set; }

        public ExperimentSelectedEventArgs(int experimentID)
        {
            this.SelectedExperimentID = experimentID;
        }
    }

    public delegate void ExperimentSelectedEventHandler(object sender, ExperimentSelectedEventArgs e);

    public partial class ExperimentQuickSearch : UserControl
    {
        public ExperimentSelectedEventHandler ExperimentSelected;

        public string SearchLabelText { get { return this.lbl_QuickSearchTitle.Text; } set { this.lbl_QuickSearchTitle.Text = value; } }
        public string OKButtonText { get { return this.btn_OK.Text; } set { this.btn_OK.Text = value; } }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.ConfigureEvents();
        }

        private void ConfigureEvents()
        {
            this.btn_OK.Click += new EventHandler(btn_OK_Click);
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            string selectedExperimentIDstr = this.txt_ExperimentAutoComplete.Text;
            int selectedExperimentID;
            if (int.TryParse(selectedExperimentIDstr, out selectedExperimentID))
            {
                this.ExperimentSelected(this, new ExperimentSelectedEventArgs(selectedExperimentID));
            }
            else
            {
                this.lbl_ErrorLabel.Text = "Experiment ID required";
            }
        }
    }
}
